﻿@page "/login"
@layout LoginLayout
@using System.ComponentModel.DataAnnotations
@inject NavigationManager NavigationManager
@inject IRBACUserService RBACService
@inject NotificationService NotificationService

<div class="logincss">
    <RadzenStack Orientation="Orientation.Vertical">
        <RadzenText TextStyle="TextStyle.H4" Style="margin: 30px" class="rz-color-white">图灵科技</RadzenText>
        <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Center" AlignItems="AlignItems.Center">
            <RadzenStack Orientation="Orientation.Vertical">
                <RadzenText TextStyle="TextStyle.H1" class="rz-color-white">图灵进销存管理</RadzenText>
                <RadzenImage Path="img/login-form.2c0d1b11.png" />
            </RadzenStack>

            <RadzenCard class="rz-p-0" style="width: 400px; margin: 0px 150px 150px 50px;">
                <EditForm Model="@loginModel" OnValidSubmit="HandleLogin">
                    <DataAnnotationsValidator />
                    <RadzenStack Orientation="Orientation.Vertical">
                        <RadzenCard style="margin: 10px 0px 0px 0px">
                            <RadzenText TextStyle="TextStyle.H6" class="rz-color-black">欢迎登录</RadzenText>
                        </RadzenCard>

                        <div class="form-group" style="margin: 10px 15px 0px 15px; padding: 5px">
                            <label>用户名：</label>
                            <InputText @bind-Value="loginModel.Account" class="form-control" />
                            <ValidationMessage For="@(() => loginModel.Account)" />
                        </div>

                        <div class="form-group" style="margin: 0px 20px 10px 20px;">
                            <label>密码：</label>
                            <InputText @bind-Value="loginModel.Password" type="password" class="form-control" />
                            <ValidationMessage For="@(() => loginModel.Password)" />
                        </div>

                        <RadzenButton Icon="Login" 
                                      ButtonType="ButtonType.Submit" 
                                      BusyText="验证中..."
                                      Text="登录" 
                                      IsBusy=@isLoading class="btn btn-primary" 
                                      style="margin: 10px 15px 20px 15px; height:30px;"/>                                                    
                    </RadzenStack>

                    @if (!string.IsNullOrEmpty(ErrorMessage))
                    {
                        <div class="alert alert-danger mt-3">@ErrorMessage</div>
                    }
                </EditForm>
            </RadzenCard>
        </RadzenStack>
    </RadzenStack>
</div>

@code {
    private LoginModel loginModel = new();
    private string ErrorMessage = string.Empty;
    private bool isLoading = false;

    private async Task HandleLogin()
    {
        isLoading = true;
        ErrorMessage = string.Empty;
        try
        {
            var userdto = new UserLoginDto
            {
                Account = loginModel.Account,
                Password = loginModel.Password
            };
            var user = await RBACService.Login(userdto);
            if (user != null)
            {
                AppSession.User = user;
                NavigationManager.NavigateTo("/");
            }                
            else
            { 
                NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = "用户名或密码错误" });
            }
        }
        catch(Exception ex)
        {
            NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = $"登录过程中发生错误{ex.Message}" });
        }
        finally
        {
            isLoading = false;
        }
    }

    private class LoginModel
    {
        [Required(ErrorMessage = "用户名必填")]
        public string? Account { get; set; } = "admin";

        [Required(ErrorMessage = "密码必填")]
        [DataType(DataType.Password)]
        public string? Password { get; set; } = "123456";
    }
}